package com.atzly.community.datasource;

import com.atzly.community.datasource.config.MyProps;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.DateSerializer;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;

@Slf4j
@Data
@Configuration
@EnableConfigurationProperties(MyProps.class)
//@MapperScan(basePackages = "com.huice.hjy.common.mapper")
public class MyConfig implements WebMvcConfigurer, ApplicationContextAware  {

	@Value("${spring.profiles.active:NA}")
	private String activeProfile;

	@Value("${spring.application.name:NA}")
	private String appName;

	@Autowired
	MyProps myProps;

//	@Value("${spring.application.appId}")
	private Long appId = 123456L;

	@Value("${server.port}")
	private Integer port ;

//	@Bean
//	public ModelMapper modelMapper() {
//		return new ModelMapper();
//	}
//
//
//	@Bean
//	public EnvConfig envConfig() {
//		return EnvConfig.getEnvConfg(activeProfile);
//	}
//
//	@Bean
//	public SentryClient sentryClient() {
//
//		SentryClient sentryClient = Sentry.init(hjyProps.getSentryDsn());
//		sentryClient.setEnvironment(activeProfile);
//		sentryClient.setRelease(hjyProps.getDeployEnv());
//		sentryClient.addTag("service", appName);
//
//		return sentryClient;
//	}
//
//	@Override
//	public void addInterceptors(InterceptorRegistry registry) {
//		registry.addInterceptor(new AuthorizeInterceptor());
//	}
//
//	@Bean
//	public RequestInterceptor feignRequestInterceptor() {
//		return new FeignRequestHeaderInterceptor(  );
//	}
//
//	@Bean
//	public MetaObjectHandler metaObjectHandler() {
//		return new CommonFieldHandler();
//	}
//
//	@Bean
//	public MybatisPlusInterceptor mybatisPlusInterceptor() {
//		MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
//
//		TenantLineInnerInterceptor tenantLineInterceptor = new HjyTenantLineInnerInterceptor();
//		tenantLineInterceptor.setTenantLineHandler(new HjyTenantLineHandler());
//		interceptor.addInnerInterceptor(tenantLineInterceptor);
//
//		PaginationInnerInterceptor paginationInnerInterceptor = new PaginationInnerInterceptor();
//		paginationInnerInterceptor.setDbType(DbType.MYSQL);
//		paginationInnerInterceptor.setOverflow(true);
//		interceptor.addInnerInterceptor(paginationInnerInterceptor);
//
//		return interceptor;
//	}
//
//	@PostConstruct
//	public void init() {
//		// init structured logging
//		StructLog4J.setFormatter(JsonFormatter.getInstance());
//
//		// global log fields setting
//		StructLog4J.setMandatoryContextSupplier(() -> new Object[] { "env", activeProfile, "service", appName });
//	}
//
//	@PreDestroy
//	public void destroy() {
//		sentryClient().closeConnection();
//	}

	@Bean
	public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
		ObjectMapper objectMapper = builder.createXmlMapper(false).build();

		initObjectMapper(objectMapper);
		
		return objectMapper;
	}

	private void initObjectMapper(ObjectMapper objectMapper) {
		// 配置返回json数据中不包含NULL的属性
		objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		// 全局配置序列化返回 JSON 处理
		SimpleModule simpleModule = new SimpleModule();
		// JSON Long ==> String
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);

		simpleModule.addSerializer(Date.class, new DateSerializer(false, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
		simpleModule.addSerializer(Timestamp.class,
				new DateSerializer(true, new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")));
		objectMapper.registerModule(simpleModule);
	}

	@Bean
	public MappingJackson2HttpMessageConverter MappingJsonpHttpMessageConverter(Jackson2ObjectMapperBuilder builder) {

		ObjectMapper mapper = builder.build();

		initObjectMapper(mapper);

		MappingJackson2HttpMessageConverter mappingJsonpHttpMessageConverter = new MappingJackson2HttpMessageConverter(
				mapper);
		return mappingJsonpHttpMessageConverter;
	}

	@Override
	public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
		SpringContextUtil.applicationContext = applicationContext;
	}

	public static void main(String[] args) {
		InetAddress localHost = null;
		try {
			localHost = Inet4Address.getLocalHost();
		} catch (UnknownHostException e) {
		}
		String ip = localHost.getHostAddress();
		System.out.println(ip	);
	}
}
